﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization;

namespace ScriptIT
{
    static class Options
    {
        private static bool isOracle = false;
        private static int outPutType = 1;
        private static int outPutLoc = 4;
        private static string outPutFolder = string.Empty;
        private static bool openExportFile = false;
        private static bool overwriteFile = true;
        private static string customPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        private static string lastUsedFoler = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

        public static string LastUsedFoler
        {
            get { return Options.lastUsedFoler; }
            set { Options.lastUsedFoler = value; }
        }

        public static string CustomPath
        {
            get { return Options.customPath; }
            set { Options.customPath = value; }
        }
        public static bool OpenExportFile
        {
            get { return Options.openExportFile; }
            set { Options.openExportFile = value; }
        }

        public static bool OverwriteFile
        {
            get { return Options.overwriteFile; }
            set { Options.overwriteFile = value; }
        }

        public enum eOutPutType
        {
            SingleScriptFile = 0,
            Separatefiles= 1,
            SingleZipFile = 2,
            EmailSingleZipFile = 3
        }
        public enum eDefaultLocType
        {
            LastUsedFolder = 0,
            MyDocuments =1,
            Desktop = 2,
            Custom = 3,
            AskEveryTime = 4
        }
        public static bool IsOracle
        {
            get { return Options.isOracle; }
            set { Options.isOracle = value; }
        }
        public static int OutPutType
        {
            get { return Options.outPutType; }
            set { Options.outPutType = value; }
        }
        public static int OutPutLocation
        {
            get { return Options.outPutLoc; }
            set { Options.outPutLoc = value; }
        }
        public static string OutPutFolder
        {
            get { return Options.outPutFolder; }
            set { Options.outPutFolder = value; }
        }
        public static void SaveOptions()
        {
            XmlDocument config = new XmlDocument();
            string configFileName = System.Environment.CurrentDirectory + "\\" + "Options.Config";
            if (!File.Exists(configFileName))
            {
                FileStream oFile= File.Create(configFileName);
                oFile.Close();

                XmlNode mainNode = config.CreateXmlDeclaration("1.0", "UTF-8", null);
                config.AppendChild(mainNode);
                XmlNode configNode = config.CreateElement("Options");
                config.AppendChild(configNode);
                config.Save(configFileName);
            }
            else
                config.Load(configFileName);

            XmlNode tableNode = config.SelectSingleNode("Options");
            XmlAttribute Key;
            //Is Oracle
            Key = config.CreateAttribute("IsOracle");
            Key.Value = isOracle.ToString();
            tableNode.Attributes.Append(Key);

            //OutputType
            Key = config.CreateAttribute("OutputType");
            Key.Value = outPutType.ToString();
            tableNode.Attributes.Append(Key);

            //OutPutLoc
            Key = config.CreateAttribute("OutPutLoc");
            Key.Value = outPutLoc.ToString();
            tableNode.Attributes.Append(Key);

            //OutPutFolder
            Key = config.CreateAttribute("OutPutFolder");
            Key.Value = outPutFolder.ToString();
            tableNode.Attributes.Append(Key);
            
            //Open Export File
            Key = config.CreateAttribute("OpenExportFile");
            Key.Value = openExportFile.ToString();
            tableNode.Attributes.Append(Key);
            
            //Overwrite File
            Key = config.CreateAttribute("OverwriteFile");
            Key.Value = overwriteFile.ToString();
            tableNode.Attributes.Append(Key);

            //Custom Folder
            Key = config.CreateAttribute("CustomFolder");
            Key.Value = customPath.ToString();
            tableNode.Attributes.Append(Key);

            //Last Used Folder
            Key = config.CreateAttribute("LastUsedFolder");
            Key.Value = lastUsedFoler.ToString();
            tableNode.Attributes.Append(Key);

            config.Save(configFileName);

        }
        public static void LoadOptions()
        {
            XmlDocument config = new XmlDocument();
            string configFileName = System.Environment.CurrentDirectory + "\\" + "Options.Config";
            config.Load(configFileName);
            XmlNode tableNode = config.SelectSingleNode("Options");
            XmlAttribute key = tableNode.Attributes["IsOracle"];
            if (key != null)
            {
                Options.IsOracle = (key.Value.ToUpper() == "TRUE");
            }
            key = tableNode.Attributes["OutputType"];
            if (key != null)
            {
                int outType = 0;
                int.TryParse(key.Value,out outType);
                Options.OutPutType = outType;
            }
            key = tableNode.Attributes["OutPutLoc"];
            if (key != null)
            {
                int outType = 0;
                int.TryParse(key.Value, out outType);
                Options.OutPutLocation = outType;
            }
            key = tableNode.Attributes["OutPutFolder"];
            if (key != null)
            {
                Options.OutPutFolder = key.Value;
            }
            key = tableNode.Attributes["OpenExportFile"];
            if (key != null)
            {
                Options.OpenExportFile = (key.Value.ToUpper() == "TRUE");
            }
            key = tableNode.Attributes["OverwriteFile"];
            if (key != null)
            {
                Options.OverwriteFile = (key.Value.ToUpper() == "TRUE");
            }
            key = tableNode.Attributes["CustomFolder"];
            if (key != null)
            {
                Options.CustomPath = key.Value;
            }
            key = tableNode.Attributes["LastUsedFolder"];
            if (key != null)
            {
                Options.LastUsedFoler = key.Value;
            }
        }
    }

}
